package com.hzit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hzit.dto.StudentDTO;
import com.hzit.entity.Classes;
import com.hzit.entity.PageResult;
import com.hzit.entity.Student;
import com.hzit.entity.StudentExample;
import com.hzit.mapper.ClassesMapper;
import com.hzit.mapper.StudentMapper;
import com.hzit.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
public class StudentServiceImpl implements IStudentService {

	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private ClassesMapper classesMapper;

	//1. 查询所有学生
	@Override
	public List<StudentDTO> findAll() {
		//1.1 查询所有学生列表
		List<Student> students = studentMapper.selectByExample(null);
		//1.2 将学生列表转换为List<StudentDTO>对象并返回
		return students.stream().map(m -> {
			StudentDTO dto = new StudentDTO();
			BeanUtil.copyProperties(m,dto,true);
			Classes classes = classesMapper.selectByPrimaryKey(m.getCid());
			dto.setCname(classes.getCname());
			return dto;
		}).collect(Collectors.toList());
	}

	//2. 分页查询
	@Override
	public PageResult<StudentDTO> findPage(int page, int pageSize) {
		//2.1 开始分页
		PageHelper.startPage(page,pageSize);
		//2.2 查询所有学生
		StudentExample example = new StudentExample();
		List<Student> students = studentMapper.selectByExample(example);
		//2.3 转换为Page对象
		Page<Student> studentPage = (Page<Student>) students;
		//2.4 得到分页的每页记录集合
		List<Student> result = studentPage.getResult();
		//2.5 转换为List<StudentDTO>
		List<StudentDTO> collect = result.stream().map(m -> {
			StudentDTO dto = new StudentDTO();
			BeanUtil.copyProperties(m, dto, true);
			dto.setCname(classesMapper.selectByPrimaryKey(m.getCid()).getCname());
			return dto;
		}).collect(Collectors.toList());
		//2.6 返回分页结果
		return new PageResult<>(collect,studentPage.getTotal());

	}

	//3. 条件带分页查询
	@Override
	public PageResult<StudentDTO> search(int page, int pageSize, StudentDTO dto) {
		//3.1 开始分页
		PageHelper.startPage(page,pageSize);
		//3.2 封装查询条件
		StudentExample example = new StudentExample();
		StudentExample.Criteria criteria = example.createCriteria();
		if(dto != null){
			if(StrUtil.isNotBlank(dto.getSname())){
				criteria.andSnameLike("%" + dto.getSname() + "%");
			}
			if(StrUtil.isNotBlank(dto.getAddr())){
				criteria.andAddrLike("%" + dto.getAddr() + "%");
			}
			if(dto.getCid() != null && dto.getCid() != 0){
				criteria.andCidEqualTo(dto.getCid());
			}
		}
		//3.3 开始条件查询
		List<Student> students = studentMapper.selectByExample(example);
		//3.4 转换为Page对象
		Page<Student> studentPage = (Page<Student>) students;
		//3.5 得到分页的每页记录集合
		List<Student> result = studentPage.getResult();
		//3.6 转换为List<StudentDTO>
		List<StudentDTO> collect = result.stream().map(m -> {
			StudentDTO studentDTO = new StudentDTO();
			BeanUtil.copyProperties(m, studentDTO, true);
			studentDTO.setCname(classesMapper.selectByPrimaryKey(m.getCid()).getCname());
			return studentDTO;
		}).collect(Collectors.toList());
		//3.7 返回分页结果
		return new PageResult<>(collect,studentPage.getTotal());
	}

	//4. 添加学生
	@Override
	public void insert(StudentDTO dto) {
		//4.1 将dto转换为student对象
		Student student = new Student();
		BeanUtil.copyProperties(dto,student,true);
		studentMapper.insert(student);
	}
	//5. 修改学生
	@Override
	public void update(StudentDTO dto) {
		//5.1 将dto转换为student对象
		Student student = new Student();
		BeanUtil.copyProperties(dto,student,true);
		studentMapper.updateByPrimaryKey(student);
	}

	//6. 删除学生
	@Override
	public void delete(int sid) {
		studentMapper.deleteByPrimaryKey(sid);
	}
}
